﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Modules;
using Maverick.Domain.Abstract;
using Maverick.Domain.Concrete;

namespace Maverick.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel kernel = new StandardKernel(new SportsStoreServices());

        protected override IController GetControllerInstance(RequestContext context, Type controllerType)
        {
            if (controllerType == null)
            {
                return null;
            }
            return (IController)kernel.Get(controllerType);
        }

        private class SportsStoreServices : NinjectModule
        {
            public override void Load()
            {
                Bind<IMessagesRepository>()
                    .To<SqlMessagesRepository>()
                    .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AppDb"]
                    .ConnectionString);
                Bind<IConfigurationRepository>()
                    .To<SqlConfigurationRepository>()
                    .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AppDb"]
                    .ConnectionString)                    ;
            }
        }
    }
}
